﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Reflection;
using FinPlusCommon;
using FinPlusWPFControls;
using log = Logger.Logger;
using u = FinPlusUtility.Utility;

namespace FinPlusDesktop
{
    //UI logic
    public partial class MainWindow : Window, IFinPlusComp
    {
        public string Id { get { return _id == null ? "Desktop" : _id; } set { _id = value; } }
        public string[] States { get { return new string[] { "Height", "Left", "Top", "ScrollOffset" }; } }
        public string[] TestStates { get { return new string[] { "TestString", "IsValid" }; } }
        public IFunctions Adaptor { get { return _clients; } }
        public string TestString { get { return Adaptor.ToString(); } }
        public bool IsValid { get; set; }
        public string ContainerId { get; set; }

        public double ScrollOffset { get { return scrollViewer.VerticalOffset; } set { scrollViewer.ScrollToVerticalOffset(value); } }
        
        private const string XmlNode = "root/current";
        private readonly FinPlusView _finPlusApp;
        private readonly IFunctions _clients;
        private IFunction _state;
        private string _id;
        private readonly string _configPath;
        private const int _frameRate = 15;

        //construct
        public MainWindow()
        {
            InitializeComponent();

            _clients = new Functions();
            _configPath = Environment.CurrentDirectory + "\\FinPlusViews\\" + Id;
            _finPlusApp = new FinPlusView(Assembly.GetExecutingAssembly());
            Cursor = ((TextBlock)this.Resources["CPoint"]).Cursor;

            grid.Children.Add(_finPlusApp);
            LoadState();

            status.Children.Add(new FinPlusStatusBar());

            Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = _frameRate });
            _state = new Function("FinPlusDesktop", "finPlusDesktop", Environment.UserName) { Object = finPlusDesktop };
        }

        //common control interface 
        public void Dock(object component, string dock)
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public void CmdExecute(IFunction command)
        {
            IsValid = false;
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            if (_finPlusApp != null)
                _finPlusApp.Dispose();
        }

        //public
        public void SaveState()
        {
            var funcsState = FinPlusDInject.Get.ExtractStates(_state, Environment.UserName);
            funcsState.ToXmlDoc(XmlNode).Save(_configPath + ".state");
        }

        //private
        private void LoadState()
        {
            try
            {
                _state = new Function("FinPlusDesktop", "finPlusDesktop", Environment.UserName) { Object = finPlusDesktop };
                FinPlusDInject.Get.InjectStates(_state, new Functions(_configPath + ".state", XmlNode));
            }
            catch (Exception e)
            {
                log.Log(Logger.Level.Warning, e.Message, e);
            }
        }

        //events
        private void Window_Activated(object s, EventArgs a) {  }
        private void Window_Deactived(object s, EventArgs a) {this.Topmost = true; this.Activate();}
        private void Window_OnMaximize(object s, RoutedEventArgs a)
        {
            try
            {
                if (WindowState == WindowState.Maximized)
                    WindowState = WindowState.Normal;
                else if (WindowState == WindowState.Normal)
                    WindowState = WindowState.Maximized;
            }
            catch (Exception e)
            {
                log.Log(Logger.Level.Error, e.Message, e);
            }
        }
        private void Window_OnMinimize(object s, RoutedEventArgs a) {  WindowState = WindowState.Minimized; }
        private void Window_OnClose(object s, RoutedEventArgs a) { _finPlusApp.Dispose(); Close(); }
        private void Window_OnSave(object s, RoutedEventArgs a) { SaveState(); }
        private void Window_Loaded(object s, RoutedEventArgs a) { this.MaxWidth = this.MinWidth = this.ActualWidth;}
        private void Title_MouseDown(object s, MouseButtonEventArgs a){ try{ this.DragMove();}catch{}}
        private void Scroll_Up(object s, MouseButtonEventArgs a) { scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - 20); }
        private void Scroll_Down(object s, MouseButtonEventArgs a) { scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + 20); }
    }
}
